

function dec2bin(decNumber, binBit) {
    let binbit = (!!binBit) ? parseInt(binBit) : 0
    let dec = []
    if (decNumber == 0) {
        dec.unshift(0)
    }
    while (decNumber > 0) {
        dec.unshift(decNumber % 2)
        decNumber = parseInt(decNumber / 2)
        if (decNumber == 0) break
    }
    while (binbit > dec.length) {
        dec.unshift(0)
    }
    return dec.join("")
}

transbtn.addEventListener("click", function () {
    let decvalue = dec.value
    let binvalue = bin.value
    if (decvalue == "" || decvalue < 0 || decvalue.indexOf(".") != -1 || binvalue < 0 || binvalue.indexOf(".") != -1) {
        binresult.innerHTML = "请输入正确的数"
        dec.value = ""
        bin.value = ""
    } else {
        let result = dec2bin(decvalue, binvalue)
        binresult.innerHTML = "计算结果： " + result
    }
})

function isNumber(params) {
    let p = params.trim()
    let pattern = /^-?\d+$|^-?\d+\.\d+$/
    return pattern.test(p)
}

function getCheckedtext() {
    if (radioa.checked) {
        return numa.value.trim()
    } else if (radiob.checked) {
        return numb.value.trim()
    } else {
        return false
    }
}

for (let index = 0; index < btns.length; index++) {
    const element = btns[index];
    element.addEventListener("click", function (e) {
        const target = e.target
        const text = target.innerHTML
        if (text == "判断当前选中的输入框输入内容是否为数字") {
            if (getCheckedtext() !== false) {
                result.innerHTML = isNumber(getCheckedtext()) ? "是数字" : "不是数字"
            } else {
                result.innerHTML = "请先选择需要判断的输入框"
            }
        } else if (text == "把 A 四舍五入为 B 个小数位数的数字") {
            if (isNumber(numa.value)) {
                if (isNumber(numb.value) && numb.value >= 0) {
                    result.innerHTML = Number(numa.value).toFixed(numb.value)
                } else if (numb.value.trim() == "") {
                    result.innerHTML = numa.value
                }
            } else {
                result.innerHTML = "输入有误"
            }
        } else if (text == "当前选中数字的绝对值") {
            if (getCheckedtext() !== false) {
                result.innerHTML = isNumber(getCheckedtext()) ? Math.abs(getCheckedtext()) : "输入有误"
            } else {
                result.innerHTML = "请先选择需要判断的输入框"
            }
        } else if (text == "对当前选中的数字进行上舍入") {
            if (getCheckedtext() !== false) {
                result.innerHTML = isNumber(getCheckedtext()) ? Math.ceil(getCheckedtext()) : "输入有误"
            } else {
                result.innerHTML = "请先选择需要判断的输入框"
            }
        } else if (text == "对当前选中的数字进行下舍入") {
            if (getCheckedtext() !== false) {
                result.innerHTML = isNumber(getCheckedtext()) ? Math.floor(getCheckedtext()) : "输入有误"
            } else {
                result.innerHTML = "请先选择需要判断的输入框"
            }
        } else if (text == "把当前选中的数字四舍五入为最接近的整数") {
            if (getCheckedtext() !== false) {
                result.innerHTML = isNumber(getCheckedtext()) ? Math.round(getCheckedtext()) : "输入有误"
            } else {
                result.innerHTML = "请先选择需要判断的输入框"
            }
        } else if (text == "返回 A 和 B 中的最高值") {
            if (isNumber(numa.value) && isNumber(numb.value)) {
                result.innerHTML = Math.max(numa.value, numb.value)
            } else {
                result.innerHTML = "输入有误"
            }
        } else if (text == "返回 A 和 B 中的最低值") {
            if (isNumber(numa.value) && isNumber(numb.value)) {
                result.innerHTML = Math.min(numa.value, numb.value)
            } else {
                result.innerHTML = "输入有误"
            }
        }
    })
}